Selectively load any method or class that seems useful.
DESCRIPTION
DB example code.pgs contains the example code fragments used as part of the Database Engine primitives documentation, as well as a few other useful and illustrative methods. It is deliberately kept very simple: it assumes that at any one time there is only one database, one table and one key that is open, the path ID of which can be accessed in the persistent "current db", "current table" and "current key" respectively. The videos.txt file is a text-only file with a few videos records in tab-delimited format. This file was imported into the videosDB database via the "table-import" primitive, into the "videos" table. Each cluster of that table is an instance of class "videos".
The "//error?" method referred to in the 2.5 Updates Manual is in class "path". This class contains a few attributes which are inherited by its three subclasses, "database", "table" and "key", which contain further attributes which could be used to implement a more sophisticated database shell. All the code fragments used in the database primitives docs are contained in class "database". These methods can be used to do basic manipulation of new or existing databases; they do not constitute a finished example (only the most basic I/O, with no System Class-based user interface, is provided).
PERSISTENTS
current-DB
current-table
current-key The videos database provided has two key attributes, "Artist" (string, case-insensitive by default) and ID (natural number (integer > 0)). Be sure to set the current-key to be the key you want to work with.
current-cluster In the example code provided, current-cluster is used only to store the cluster ID of the currently referenced cluster.
CLASS "path"
• Instance Attributes: name, ID, owner
Database entities have names (useful on the user level), path IDs (required by the database primitives), and owners (databases could belong to an application, tables belong to databases, and keys belong to tables).
• error?
Succeeds if error number 0 (database operation succeeded) is reported. Otherwise it fails, after indicating whether a file (error # < 0) or database (error # > 0) occurred. This failure usually triggers immediate termination of the method in which it occurs. One of the first improvements that could be made, of course, would be to provide a full-text error message for each database error that can occur.
CLASS "database" (inherits from "path")
• Class Attributes: databases, current DB
The class attribute "databases" could be used to store a list of databases, while "current DB" could be used to refer to the (one) database currently in use (this would provide an alternative and somewhat more flexible way of referring to paths than that provided by using persistents). A further extension would be to maintain a library list and an "active" (those that are open) list of databases.
• Instance Attribute: tables
A list of tables belonging to the database.
• find all by key
Finds all the clusters that have the same value for the key "Artist". Asks the user for that value, and then puts together a list of cluster values (instances of class "videos"), which, after telling the user how many clusters it found, it displays via the "display" primitive. Note that the comparison routine it uses to determine whether the key being read by "key-next" is still the one requested by the user is a case-insensitive string comparison routine; this routine will not work with a key that is not a string.
This method assumes you have 1) opened the "videosDB" database (use "open db"), 2) opened the "videos" table (use "open table"), and 3) opened the "Artist" key (use "open key").
• list all keys
List all the keys for the "videos" table (eliminating redundant entries).